
// Copyright (c) 2012, <OWNER>
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: 
// 
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer. 
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// The views and conclusions contained in the software and documentation are those
// of the authors and should not be interpreted as representing official policies, 
// either expressed or implied, of the FreeBSD Project.
// 
// 

#include <AmiLib.h>
#include <AmiDxeLib.h>
#include <AmiCspLib.h>
#include "Setup.h"
#include "AMIVfr.h"						
#if EFI_SPECIFICATION_VERSION>0x20000
    #include <Protocol/HiiString.h>
    #include <Protocol/HiiDatabase.h>
#else
    #include <Protocol/Hii.h>
#endif
#include <SetupStrTokens.h>
#include "Include\Setup.h"			


extern VOID OEM_HHMCommon(
    IN      UINT64    RegData,
    IN      UINT8     Func,
    IN      UINT16    StrToken,
    IN      UINT8     OddPos,
    IN      EFI_HII_HANDLE    hiiHandle
);
typedef UINT8	uint8_t;
typedef UINT16	uint16_t;
typedef UINT32	uint32_t;
typedef UINT64	uint64_t;
typedef INT8	int8_t;
typedef INT16	int16_t;
typedef INT32	int32_t;
typedef INT64	int64_t;
extern uint32_t read_tach(uint8_t idx);
extern uint32_t read_peci_temp(uint8_t idx);
extern uint32_t read_fw_ver(void);
extern uint32_t check_fw_signature(void);
extern void ast1300_ctx_enter(void);
extern void ast1300_ctx_leave(void);
extern int64_t get_voltage_value(uint32_t idx);
extern uint32_t read_i2c_temp(uint8_t pwm_idx, uint8_t i2c_idx);

#define VOLTAGE             0x01
#define TEMPERATURE         0x02
#define FAN_SPEED           0x03

#define UPDATE_FAN(i, i2)       \
    data_value = read_tach(i);\
	TRACE((-1, "FAN%d = %d,", i, data_value));\
    Token = STRING_TOKEN(STR_OEM_AST_FAN##i2##_SPEED_VALUE);\
    OEM_HHMCommon(data_value, FAN_SPEED, Token, OddPos, HiiHandle);

#define UPDATE_ADC(i)\
    OddPos = 3;\
    data_value = get_voltage_value(i); \
    Token = STRING_TOKEN(STR_OEM_AST_VIN##i##_VALUE);\
    OEM_HHMCommon(data_value, VOLTAGE, Token, OddPos, HiiHandle);

void AST1300_HWM_CB(
    IN EFI_HII_HANDLE HiiHandle, 
    IN UINT16 Class, 
    IN UINT16 SubClass, 
    IN UINT16 Key
)
{  
    EFI_STATUS         Status;
    STRING_REF         Token;          //String token
    UINT8	           OddPos;         //Define how much is odd
    UINT64              data_value;
	EFI_TIME			time;	
	
	pRS->GetTime(&time, NULL);
	TRACE((-1, "TS: %d:%d:%d %d.%d.%d\n", time.Year, time.Month, time.Month, time.Hour, time.Minute, time.Second));
    TRACE((-1, "==> %s\n", __FUNCTION__));
    ast1300_ctx_enter();
    OddPos = 0x00;
    (void)Status;
    // CPU Temp via PECI.
    data_value = read_peci_temp(0);
	TRACE((-1, "peci0 temp = %d\n", data_value));
    if (data_value != -1) {
        Token = STRING_TOKEN(STR_OEM_AST_CPU0_TEMP_VALUE);
        OEM_HHMCommon((UINT8)data_value, TEMPERATURE, Token, OddPos, HiiHandle);
    }
    data_value = read_peci_temp(1);
	TRACE((-1, "peci1 temp = %d\n", data_value));
    if (data_value != -1) {
        Token = STRING_TOKEN(STR_OEM_AST_CPU1_TEMP_VALUE);
        OEM_HHMCommon((UINT8)data_value, TEMPERATURE, Token, OddPos, HiiHandle);
    }
	data_value = read_i2c_temp(2, 0);
	TRACE((-1, "sio temp0 = %d\n", data_value));
    if (data_value != 0) {
        Token = STRING_TOKEN(STR_OEM_AST_SIO_TEMP0_VALUE);
        OEM_HHMCommon((UINT8)data_value, TEMPERATURE, Token, OddPos, HiiHandle);
    }
	data_value = read_i2c_temp(2, 1);
	TRACE((-1, "sio temp1 = %d\n", data_value));
    if (data_value != 0) {
        Token = STRING_TOKEN(STR_OEM_AST_SIO_TEMP1_VALUE);
        OEM_HHMCommon((UINT8)data_value, TEMPERATURE, Token, OddPos, HiiHandle);
    }
	data_value = read_i2c_temp(2, 2);
	TRACE((-1, "sio temp2 = %d\n", data_value));
    if (data_value != 0) {
        Token = STRING_TOKEN(STR_OEM_AST_SIO_TEMP2_VALUE);
        OEM_HHMCommon((UINT8)data_value, TEMPERATURE, Token, OddPos, HiiHandle);
    }

    UPDATE_FAN(0, 1);
    UPDATE_FAN(1, 2);
    UPDATE_FAN(2, 3);
    UPDATE_FAN(3, 4);
    UPDATE_FAN(4, 5);
    UPDATE_FAN(5, 6);
    
    UPDATE_ADC(0);
    UPDATE_ADC(1);
    UPDATE_ADC(2);
    UPDATE_ADC(3);
    UPDATE_ADC(4);
    UPDATE_ADC(5);
    UPDATE_ADC(6);
    UPDATE_ADC(7);
    UPDATE_ADC(8);
    UPDATE_ADC(9);
    UPDATE_ADC(10);
    UPDATE_ADC(11);

    ast1300_ctx_leave();
    return ;
}

